
package ch.hearc.coursJava.IO.RMI.chat;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ch.hearc.coursJava.GUI.frame.tools.JFrameBase;
import ch.hearc.coursJava.IO.RMI.exemple0.pcB.moo.StringCrypte;

import com.bilat.tools.reseau.rmi.RmiTools;

public class JFrameChat extends JFrameBase implements Chat_I
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JFrameChat(RMIData dataServeur, RMIData dataClient)
		{
		creerComposant();
		addComposant();
		addListener();

		setPropriete();

		serveur(dataServeur);

		client(dataClient);
		}

	private void client(RMIData dataClient)
		{
		this.rmiClient = (Chat_I)RmiTools.connectionRemoteObjectBloquant(dataClient.getIp().getHostName(), dataClient.getId(), dataClient.getPort());
		}

	private void serveur(RMIData dataServeur)
		{
		try
			{
			RmiTools.shareObject(this, dataServeur.getPort(), dataServeur.getId());
			}
		catch (RemoteException e)
			{
			e.printStackTrace();
			}
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	@Override
	public void put(StringCrypte message) throws RemoteException
		{
		this.affichage.append("\r\n" + message);
		}

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void addComposant()
		{
		this.setLayout(new BorderLayout());
		this.add(boxCentrale, BorderLayout.CENTER);
		boxCentrale.add(saisie);
		boxCentrale.add(affichage);
		this.add(btnSend, BorderLayout.SOUTH);
		}

	private void creerComposant()
		{
		this.saisie = new JTextField();
		this.affichage = new JTextArea();
		this.boxCentrale = Box.createVerticalBox();
		this.btnSend = new JButton("Send");
		}

	private void addListener()
		{
		this.btnSend.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent arg0)
					{
					try
						{
						rmiClient.put(new StringCrypte(saisie.getText()));
						}
					catch (RemoteException e)
						{
						e.printStackTrace();
						}
					saisie.setText("");
					}
			});
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	private JTextField saisie;
	private JTextArea affichage;
	private Box boxCentrale;
	private JButton btnSend;
	private Chat_I rmiClient;
	}
